package models;

import controllers.chat.ChatController;
import models.chat.Channel;
import models.chat.Status;
import play.Logger;
import play.db.jpa.Model;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Konovalov_Nik
 */
@Entity
public class ApplicationUser extends Model {
    public String openId;

    @ElementCollection
    public Map<String, String> extensions;

    public String email;
    public String name;
    public String dateOfBirthday;
    public String country;
    public String language;

    @ElementCollection
    public List<Long> channelIds = new ArrayList<Long>();
    public Status status;

    public ApplicationUser(String openId, Map<String, String> extensions) {
        this.openId = openId;
        this.extensions = extensions;
        this.status = Status.ONLINE;

        Logger.debug(extensions.get("email"));
        Logger.debug(extensions.get("name"));
        Logger.debug(extensions.get("birthDate"));
        Logger.debug(extensions.get("country"));
        Logger.debug(extensions.get("language"));

        if (extensions.containsKey("email")) {
            email = extensions.get("email");
        }
        if (extensions.containsKey("name")) {
            name = extensions.get("name");
        }
        if (extensions.containsKey("birthDate")) {
            dateOfBirthday = extensions.get("birthDate");
        }
        if (extensions.containsKey("country")) {
            country = extensions.get("country");
        }
        if (extensions.containsKey("language")) {
            language = extensions.get("language");
        }
        if (name == null){
            if (email != null) {
                name = email.split("\\@")[0];
            } else {
                name = "unnamed";
            }
        }
    }

    public void init() {
        if (this.id == null) {
            throw new RuntimeException("not saved Application user.");
        }
        Channel global = Channel.find("byName", ChatController.GLOBAL).first();
        global.memberIds.add(id);
        global.save();

        List<ApplicationUser> allUsers = ApplicationUser.findAll();
        for (ApplicationUser member2 : allUsers) {
            if (member2.id.equals(id)) {
                continue;
            }
            String name1 = String.valueOf(id);
            String name2 = String.valueOf(member2.id);
            if (name1.compareTo(name2) == -1) {
                name1 = String.valueOf(member2.id);
                name2 = String.valueOf(id);
            }
            Channel channel = new Channel("pr-" + name1 + "-" + name2);
            channel.memberIds.add(id);
            channel.memberIds.add(member2.id);
            channel.save();
        }
    }

    public List<Channel> getChannels() {
        List<Channel> result = new ArrayList<Channel>(channelIds.size());
        for (Long channelId : channelIds) {
            result.add((Channel) Channel.findById(channelId));
        }
        return result;
    }
}
